< previous page page_473 next page >

Page 473
File-Manipulation Functions
As with the Dir$ function, most of the file-manipulation commands in Visual Basic are as straightforward as their MS-DOS equivalents, although with a few limitations. These commands are summarized in Table 21.2.
Table 21.2 Summary of File Functions
ActionSyntax
Copy a fileFileCopy source, dest
Delete one or more filesKill path
Rename a fileName oldname As newname
Create a new folderMkDir pathname
Remove an empty folderRmDir pathname
Change current directoryChDir pathname
Change current driveChDrive drive

Several of these system functions are described in the following sections.
Copying Files
The FileCopy command has the limitation that you cannot use wildcards to specify multiple files. FileCopy can copy files locally or over a network, as shown in the following examples:
 'The following line copies a file while changing its name:
FileCopy "D:\My Documents\Hey Now.txt",  "C:\DATA\TEST.TXT"

'The following lines of code use a network path for the source file:
Dim sDest As String
Dim sSource As String

 SSource = "\\myserver\deptfiles\budget98.XLS"
SDest = "C:\DATA\BUDGET.XLS"

FileCopy sSouce, sDest
The FileCopy statement automatically overwrites an existing file, unless the file is read-only or locked open by another application.
note.gif When you copy files over a network or dial-up connection, using the On Error statement to trap errors is a good idea. For example, if the network is down or another user has the file exclusively locked, your program could bomb unexpectedly. You can easily provide an error trap and retry dialog, as described in Chapter 9, "Visual Basic Programming Fundamentals." square.gif

sarrow.gifSee ''Using the On Error Statement" p. 219

 
< previous page page_473 next page >